package com.epoint.mobileoa.personalized.epointcompany.ui.kaoqing;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.tools.LogHelp;

/**
 * @author liyc
 * @time 2012-8-6 下午5:16:02
 * @annotation 
 */
public class KaoQingDetailViewTask extends Task {

	/**
	 * @param con
	 * @param param
	 */
	public static String Get_Address_Failure = "Get_Address_Failure";

	public KaoQingDetailViewTask(SuperView con, Map<String, Object> param) {
		super(con, param);
	}

	Location mylocation;

	@Override
	public void exectue(Message mess) throws Exception {


		//返回所有已知的位置提供者的名称列表，包括未获准访问或调用活动目前已停用的。
		//		List<String> lp = lm.getAllProviders();
		//		for (String item:lp)
		//		{
		//			LogHelp.LogI("可用位置服务："+item);
		//		}
		//		Criteria criteria = new Criteria(); 
		//		criteria.setCostAllowed(false); 
		//		criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置水平位置精度
		//getBestProvider只有允许访问调用活动的位置供应商将被返回
		//		String  providerName = lm.getBestProvider(criteria, true);

		String providerName = LocationManager.NETWORK_PROVIDER;

		if (providerName != null)
		{
			mylocation = KaoQingDetailView.lm.getLastKnownLocation(providerName);
			
			if(mylocation==null)
			{
				mylocation = KaoQingDetailView.klocation;
			}
			
			if(mylocation==null){
				setMessageToRefreshUI(mess, getCon(),Get_Address_Failure);
				return;
			}
			//获取维度信息
			double latitude = mylocation.getLatitude();
			//获取经度信息
			double longitude = mylocation.getLongitude();
			Geocoder geocoder = new Geocoder(getCon()); 

			List<Address> addList = null;

			//重复获取10次
			for(int i=0;i<10;i++){
				try{
					addList = geocoder.getFromLocation(latitude, longitude, 5);
				}catch(Exception e){
					e.printStackTrace();
				}
				if(addList!=null){
					break;
				}
			}

			if(addList==null){
				setMessageToRefreshUI(mess, getCon(),Get_Address_Failure);
				return;
			}
			else if(addList.size()>0){
				String addressName = "";
				Address a = addList.get(0);
				if(a!=null){
					addressName += a.getAddressLine(0)==null?"":a.getAddressLine(0);
					addressName += a.getAddressLine(1)==null?"":a.getAddressLine(1);
					addressName += a.getAddressLine(2)==null?"":a.getAddressLine(2);
					setMessageToRefreshUI(mess, getCon(),addressName);
				}
				else{
					setMessageToRefreshUI(mess, getCon(),Get_Address_Failure);
				}
			}
			else{
				setMessageToRefreshUI(mess, getCon(),Get_Address_Failure);
			}
		}
		else{
			setMessageToRefreshUI(mess, getCon(),Get_Address_Failure);
		}
	}

}
